/********************************************************************
 *
 *      Filename: MathUtil.h
 *   Description: Miscellaneous math utils
 *        Author: [kb]ypp
 *    Created at: February 20, 2005
 * Last modified: September 17, 2005
 *
 ********************************************************************/

#ifndef MATHUTIL_H
#define MATHUTIL_H

#include <cmath>
#include <cfloat>

const float EPSILON = 0.0001; // precision
const float INF = FLT_MAX; // infinity

const float PI = 3.14159265358979323846f;
const float TWO_PI = 2.0f * PI;
const float HALF_PI = PI / 2.0f;
const float ONE_OVER_PI = 1.0f / PI;
const float ONE_OVER_TWO_PI = 1.0f / TWO_PI;

const float DEG2RAD = PI / 180.0f; // degrees -> radians
const float RAD2DEG = 180.0f / PI; // radians -> degrees

// Convert degrees to radians
inline float deg2rad( float degrees )
{
	return degrees * DEG2RAD;
}

// Convert radians to degrees
inline float rad2deg( float radians )
{
	return radians * RAD2DEG;
}

/*
// "Wrap" an angle in -PI..PI range.
// Note: the given angle assumed to be in radians
inline float wrapPi( float rad )
{
	rad += PI;
	rad -= floor( rad * ONE_OVER_TWO_PI ) * TWO_PI;
	rad -= PI;
	return rad;
}
*/

/*
// "Wrap" an angle in -180..180 range.
// Note: the given angle assumed to be in degrees
inline float wrap180( float deg )
{
	deg += 180.0;
	deg -= floor( deg / 360.0 ) * 360.0;
	deg -= 180.0;
	return deg;
}
*/

// Sign function:
//             { -1, x < 0
// sign( x ) = {  0, x == 0
//             {  1, x > 0
inline float sign( float value )
{
	if ( value == 0.0 ) // FIXME: operation==() for floats is not reliable
		return 0.0;
	return ( value > 0.0 ? 1.0 : -1.0 );
}

#endif // MATHUTIL_H
